//
//  MQStatusRetweetView.swift
//  MQWeibo
//
//  Created by 毛强 on 16/1/1.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit

class MQStatusRetweetView: UIView {
    
    var bottomConstraint:Constraint?
    
    var statusViewModel: MQStatusViewModel? {
        didSet{
            contentLabel.text = statusViewModel?.retweetStatusText
            
            bottomConstraint?.uninstall()
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls where picUrls.count > 0{
                statusPictureView.hidden = false
                statusPictureView.picUrls = picUrls
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(statusPictureView.snp_bottom).offset(MQStatusCellMargin).constraint
                })
            }else{
                statusPictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(MQStatusCellMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        addSubview(contentLabel)
        addSubview(statusPictureView)
        
        // 添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(MQStatusCellMargin)
            make.top.equalTo(self).offset(MQStatusCellMargin)
            make.width.lessThanOrEqualTo(self.snp_width).offset(2 * -MQStatusCellMargin)
        }
        
        statusPictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(MQStatusCellMargin)
            make.leading.equalTo(contentLabel)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(statusPictureView).offset(MQStatusCellMargin).constraint
        }
    }
    
    
    // MARK: - 懒加载
    
    // 内容的控件
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
        // 多行
        label.numberOfLines = 0
        return label
    }()
    private lazy var statusPictureView:MQStatusPictureView={
        let pictureView = MQStatusPictureView()
        pictureView.backgroundColor = self.backgroundColor
        return pictureView
    }()
}
